package com.mall.order.mq.consumer;

import com.mall.order.constant.OrderRetCode;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.Stock;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.StockMapper;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.PostConstruct;
import java.util.List;

import static org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
import static org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus.RECONSUME_LATER;

@Component
public class DelayOrderConsumer {
    private DefaultMQPushConsumer consumer;

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    StockMapper stockMapper;

    @PostConstruct
    public void init(){
        consumer = new DefaultMQPushConsumer("delay-order-consumer");
        consumer.setNamesrvAddr("127.0.0.1:9876");
        try {
            consumer.subscribe("delayOrder","*");
            consumer.registerMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                    MessageExt msg = list.get(0);
                    byte[] body = msg.getBody();
                    String orderId = new String(body);
                    //查询订单是否已支付,已支付直接返回成功,未支付,删除订单再返回
                    try {
                        Example example = new Example(OrderItem.class);
                        Example.Criteria criteria = example.createCriteria();
                        criteria.andEqualTo("orderId", orderId);
                        List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
                        Order order = orderMapper.selectByPrimaryKey(orderId);
                        if(0 == order.getStatus()){
                            order.setStatus(5);
                            orderMapper.updateByPrimaryKey(order);
                            for (OrderItem orderItem : orderItems) {
                                OrderItem  orderItemtemp=orderItem;
                                orderItemtemp.setStatus(2);
                                orderItemMapper.updateByPrimaryKey( orderItemtemp);
                                //库存变化
                                Stock stock = stockMapper.selectStock(orderItem.getItemId());
                                //锁定库存
                                stock.setLockCount(stock.getLockCount() - orderItem.getNum());
                                //库存
                                stock.setStockCount(stock.getStockCount() + orderItem.getNum());

                                stockMapper.updateByPrimaryKey(stock);
                            }
                        }
                        return CONSUME_SUCCESS;
                    } catch (Exception e) {
                        return RECONSUME_LATER;
                    }
                }
            });
            consumer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }

    }
}
